package com.example.oms.service.impl;

import com.example.oms.dao.*;
import com.example.oms.dto.ReserveDto;
import com.example.oms.dto.ReserveOfflineDto;
import com.example.oms.entity.*;
import com.example.oms.service.PatientService;
import com.example.oms.vo.DiagReportsInfoVo;
import com.example.oms.vo.DoctorArrangeInfoVo;
import com.example.oms.vo.PrescriptionsInfoVo;
import com.example.oms.vo.RegistrationInfoVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
public class PatientServiceImpl implements PatientService {
    @Resource
    PatientsDao patientsDao;
    @Resource
    DepartmentsDao departmentsDao;
    @Resource
    DoctorsDao doctorsDao;
    @Resource
    ScheduleDao scheduleDao;
    @Resource
    RegistrationsDao registrationsDao;
    @Resource
    PrescriptionsDao prescriptionsDao;
    @Resource
    DiagReportsDao diagReportsDao;

    @Override
    public List<Departments> getAllDept() {
        return departmentsDao.queryAll();
    }

    @Override
    public Doctors getDoctorInfo(int id) {
        return doctorsDao.queryById(id);
    }

    @Override
    public List<DoctorArrangeInfoVo> getDoctorArrange(int deptId, String date) {
        return scheduleDao.getDoctorArrange(deptId,date);
    }

    @Override
    public int getReserveNum(ReserveDto reserveDto) {
        return scheduleDao.getNum(reserveDto);
    }

    @Override
    public int creatRegistration(Schedule schedule,int PatientId ,int isReserve) {

        Registrations registration = new Registrations();
        registration.setPatientId(PatientId);
        registration.setDoctorId(schedule.getDoctorId());
        registration.setAppointmentPeriod(schedule.getStartTime()+":00~"+schedule.getEndTime()+":00");
        registration.setIsAppointmentRegistration(isReserve);
        registration.setAppointmentDate(schedule.getShiftDate());
        if(registrationsDao.count(registration)>=1){
            return 0;
        }
        registration.setTime(new Date());
        registration.setStatus("NotPaid");
        return registrationsDao.insert(registration);
    }

    @Override
    public void scheduleNumReduceOne(int scheduleId) {
        scheduleDao.numReduceOne(scheduleId);
    }

    @Override
    public int editPatientInfo(Patients patient) {
        return patientsDao.update(patient);
    }

    @Override
    public Patients getPatientById(Integer id) {
        return patientsDao.queryById(id);
    }

    @Override
    public Registrations getRegistrationByRegistrationId(int id) {
        return registrationsDao.queryById(id);
    }

    @Override
    public List<RegistrationInfoVo> getNotPaidRegistrationByPatientId(int id) {
        List<RegistrationInfoVo> registrationInfoVoList = registrationsDao.getNotPaidRegistrationByPatientId(id);
        for (RegistrationInfoVo registrationInfoVo : registrationInfoVoList) {
            System.out.println(registrationInfoVo.getDoctorRank());
            switch (registrationInfoVo.getDoctorRank()) {
                case "PROFESSOR":
                    registrationInfoVo.setPrice(10);
                    break;
                case "ASSOCIATE_PROFESSOR":
                    registrationInfoVo.setPrice(20);
                    break;
                case "GENERAL_PRACTITIONER":
                    registrationInfoVo.setPrice(30);
                    break;
                default:
                    registrationInfoVo.setPrice(5);
                    break;
            }
        }
        return registrationInfoVoList;
    }

    @Override
    public List<PrescriptionsInfoVo> getNotPaidPrescriptionsByPatientId(int id) {
        return prescriptionsDao.getNotPaidPrescriptionsByPatientId(id);
    }

    @Override
    public List<DiagReportsInfoVo> getNotPaidDiagReportsByByPatientId(int id) {
        return diagReportsDao.getNotPaidDiagReportsByByPatientId(id);
    }

    @Override
    public int editRegistrationsStatus(int id , String paid) {
        Registrations registrations = new Registrations();
        registrations.setId(id);
        registrations.setStatus(paid);
        return registrationsDao.update(registrations);
    }

    @Override
    public Registrations getRegistrationById(int id) {
        return registrationsDao.queryById(id);
    }

    @Override
    public int changePrescriptionStatus(int id, String paid) {
        return prescriptionsDao.changePrescriptionStatus(id,paid);
    }

    @Override
    public int changeDiagReportStatus(int id, String paid) {
        return diagReportsDao.changeDiagReportStatus(id,paid);
    }

    @Override
    public boolean patientAccountNotExist(String idNumber) {
        return patientsDao.queryByIdNumber(idNumber) == null;
    }
private Date parseDate(String s){
        try {
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
            return simpleDateFormat.parse(s);
        }catch (Exception e){
            return new Date();
        }

}
    @Override
    public int creatPatient(ReserveOfflineDto reserveOfflineDto, int id1) {
        String idNumber=reserveOfflineDto.getIdNumber();
        String name=reserveOfflineDto.getName();
        String id=id1+"";
        Date date=parseDate(idNumber.substring(6,14));
        String sex=idNumber.substring(16,17);
        if(Integer.valueOf(sex)%2==0)sex="1";
        else sex="0";
        Patients patients=patientsDao.queryById(Integer.valueOf(id));
        if(null==patients)patients=new Patients();
        patients.setId(Integer.valueOf(id));
        patients.setIdCardNumber(idNumber);
        patients.setName(name);
        patients.setBirthday(date);
        patients.setSex(Integer.valueOf(sex));
        patientsDao.update(patients);

        return patientsDao.insert(patients);
    }

    @Override
    public int getPatientIdByIdNumber(ReserveOfflineDto reserveOfflineDto) {
        return patientsDao.getPatientIdByIdNumber(reserveOfflineDto.getIdNumber());
    }

    @Override
    public Schedule getReserveById(int scheduleId) {
        return scheduleDao.queryById(scheduleId);
    }

    private LocalTime parseTime(String s){
        DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("HH:mm:ss");
        LocalTime localTime=LocalTime.parse(s,dateTimeFormatter);
    return localTime;
    }
    @Override
    public int cancelReserve(int reserveId) {
        Registrations registrations = registrationsDao.queryById(reserveId);
        registrations.setId(reserveId);
        registrations.setStatus("Canceled");
        String[]strings=registrations.getAppointmentPeriod().split("~");
        LocalTime s1=parseTime(strings[0]);
        LocalTime s2=parseTime(strings[1]);
        scheduleDao.plusOne(registrations.getDoctorId(),registrations.getAppointmentDate(),s1,s2);
        return registrationsDao.update(registrations);
    }

    @Override
    public List<RegistrationInfoVo> getRegistrationInfoByPatientId(int id) {
        return registrationsDao.queryRegistrationInfoByPatientId(id);
    }
}
